package com.jeesite.modules.rt.tp.api;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.component.CatchService;
import com.jeesite.modules.rt.dto.RouterParameter;
import com.jeesite.modules.rt.tp.service.LingwuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Calendar;

/**
 * 领雾
 */
@RestController
@RequestMapping("${adminPath}/lingwu")
public class LingwuContorller extends BaseController {


    @Autowired
    private LingwuService lingwuService;
    @Autowired
    private CatchService catchService;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 收益查询
     */
    @RequestMapping("/synShouyiInfo")
    public String synShouyiInfo(String startDate){

        if(StringUtils.isEmpty(startDate)){
            startDate  = DateUtils.getDate("yyyy-MM-dd", -1, Calendar.DATE);;
        }
        lingwuService.synShouyiInfo(startDate);

        return renderResult(Global.TRUE,"数据较多,请耐心等待");
    }

    /**
     * 同步报警信息
     */
    @RequestMapping("/synErrorInfo")
    public String synErrorInfo(){
        lingwuService.synErrorInfo();
        return renderResult(Global.TRUE,"数据较多,请耐心等待");
    }

    /**
     * 跑量查询
     */
    @RequestMapping("/synPaoliangInfo")
    public String synPaoliangInfo(){
        String startDate = DateUtils.getDate("yyyy-MM-dd", -1, Calendar.DATE);
        String endDate = DateUtils.getDate("yyyy-MM-dd", -1, Calendar.DATE);

        lingwuService.synPaoliangInfo(startDate,endDate);

        return renderResult(Global.TRUE,"数据较多,请耐心等待");
    }



    /**
     * 同步设备硬件信息
     * @return
     */
    @RequestMapping("/synYingjianInfo")
    public String synYingjianInfo(){
        String startDate = DateUtils.getDate("HH:mm:ss", -8, Calendar.MINUTE);
        String endDate = DateUtils.getDate("HH:mm:ss", -1, Calendar.MINUTE);
        //同步设备硬件信息
        lingwuService.synYingjianInfo(startDate,endDate);

        return renderResult(Global.TRUE,"数据较多,请耐心等待");
    }

    /**
     * 上行下行带宽
     * @return
     */
    @RequestMapping("/synUpDownFlow")
    public String synUpDownFlow(){

        //同步设备上行带宽
        lingwuService.synUpDownFlow();

        return renderResult(Global.TRUE,"数据较多,请耐心等待");
    }


    /**
     * 同步设备信息
     * @return
     */
    @RequestMapping("/synDeviceList")
    public String synDeviceList(){

        //麻雀云 单独定时任务
        restTemplate.getForObject("http://localhost:8040/api/deviceJob/synDeviceList", String.class);
//        String refreshTime = catchService.get(RouterParameter.CATCH_ROUTER_DEVICE_REFRESH_TIME);
//        if(StringUtils.isNotEmpty(refreshTime)){
//            if(System.currentTimeMillis()-Long.valueOf(refreshTime)<30*60*1000){
//                return renderResult(Global.FALSE,"30分钟内只能刷新一次，请不要频繁操作");
//            }
//        }
        //同步设备信息
        lingwuService.synDeviceData();



        return renderResult(Global.TRUE,"数据较多,请耐心等待");
    }

    //同步网络信息
    @RequestMapping("flowDeviceNetInfo")
    public String flowDeviceNetInfo(){
        //同步设备信息
        lingwuService.flowDeviceNetInfo();

        return renderResult(Global.TRUE,"数据较多,请耐心等待");
    }

    //同步拨号信息
    @RequestMapping("synBohaoInfo")
    public String synBohaoInfo(){
        //同步设备信息
        lingwuService.synBohaoInfo();

        return renderResult(Global.TRUE,"数据较多,请耐心等待");
    }
}
